package com.example.concurrency.exercise;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ForkJoinPool;

/**
 * @author: dangwen
 * @createDate: 2021/2/4
 */
public class CountDownLatchDemo01 {
    static CountDownLatch countDownLatch = new CountDownLatch(7);

    public static void main(String[] args) {
        for (int i = 0; i < 5; i++){
            new Thread(new SingleHandler(countDownLatch)).start();
        }
        new Thread(new MultiHandler(countDownLatch)).start();
        new Thread(new BusinessHandler(countDownLatch)).start();
        new Thread(new BusinessHandler2(countDownLatch)).start();

    }

}

class SingleHandler implements Runnable{
    private CountDownLatch countDownLatch;
    public SingleHandler(CountDownLatch countDownLatch){
        this.countDownLatch = countDownLatch;
    }
    @Override
    public void run() {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+" single----before countDown---"+countDownLatch.getCount());
        countDownLatch.countDown();
        System.out.println(Thread.currentThread().getName()+" single----after countDown---"+countDownLatch.getCount());
    }
}

class MultiHandler implements Runnable{
    private CountDownLatch countDownLatch;
    public MultiHandler(CountDownLatch countDownLatch){
        this.countDownLatch = countDownLatch;
    }
    @Override
    public void run() {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+" multi----before countDown--- first time----"+countDownLatch.getCount());
        countDownLatch.countDown();
        System.out.println(Thread.currentThread().getName()+" multi----after countDown--- first time----"+countDownLatch.getCount());
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        countDownLatch.countDown();
        System.out.println(Thread.currentThread().getName()+" multi----after countDown--- second time----"+countDownLatch.getCount());
    }
}

class BusinessHandler implements Runnable{
    private CountDownLatch countDownLatch;
    public BusinessHandler(CountDownLatch countDownLatch){
        this.countDownLatch = countDownLatch;
    }
    @Override
    public void run() {
        try {
            System.out.println(Thread.currentThread().getName()+" ----- 我是业务线程，我在等待 ----"+countDownLatch.getCount());
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+" ----- 我是业务线程，我不再等待 ----"+countDownLatch.getCount());
    }
}
class BusinessHandler2 implements Runnable{
    private CountDownLatch countDownLatch;
    public BusinessHandler2(CountDownLatch countDownLatch){
        this.countDownLatch = countDownLatch;
    }
    @Override
    public void run() {
        try {
            System.out.println(Thread.currentThread().getName()+" ----- 我是业务线程2，我在等待 ----"+countDownLatch.getCount());
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+" ----- 我是业务线程2，我不再等待 ----"+countDownLatch.getCount());
    }
}